package com.ww.web;

import com.alibaba.fastjson.JSON;
import com.ww.dao.UserDao;
import com.ww.dao.impl.UserDaoImpl;
import com.ww.entity.User;
import com.ww.util.MD5Util;
import com.ww.util.StringUtil;
import com.ww.util.UUIDUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.regex.Pattern;

@WebServlet("/user/*")
public class UserServlet extends HttpServlet {
    private UserDao userDao=new UserDaoImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String baseURL = req.getContextPath();//获取项目的基本路径
        String uri = req.getRequestURI();
        String path = uri.substring(uri.lastIndexOf("/") + 1);
        if ("login.do".equals(path)){
            login(req, resp);
            return;
        }else if ("req.do".equals(path)){
            req(req, resp);
            return;
        }else if ("member.do".equals(path)){
            HttpSession session = req.getSession();
            String user_id = null;
            if (session.getAttribute("user_id")!=null) {
                user_id = session.getAttribute("user_id").toString();
                User user = userDao.findById(user_id);
                req.setAttribute("user",user);
                req.getRequestDispatcher(baseURL+"/member.jsp").forward(req,resp);
                return;
            }else {
                req.getRequestDispatcher(baseURL+"/login.jsp").forward(req,resp);
                return;
            }

        }else {
            throw new RuntimeException("请求错误");
        }
    }

    private void req(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String name = req.getParameter("name");
        String mobile = req.getParameter("mobile");
        String password = MD5Util.MD5(req.getParameter("password"));
        String repassword = MD5Util.MD5(req.getParameter("repassword"));
        User user=new User();
        user.setUser_id(UUIDUtils.getUUID());
        user.setUser_name(name);
        user.setUser_phone(mobile);
        user.setUser_password(password);
        String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
        JSONBean jsonBean=new JSONBean();
        if (!password.equals(repassword)){
            jsonBean.setFail("两次密码不同");
        }else {
            if (!Pattern.matches(REGEX_MOBILE,mobile)){
                jsonBean.setFail("请输入正确的手机号");
            }else {
                jsonBean.setSuccess("登录成功");
                userDao.save(user);
            }
        }
        resp.setContentType("application/json;charset=utf-8");
        String json = JSON.toJSONString(jsonBean);
        PrintWriter out = resp.getWriter();
        out.write(json);
        out.close();
        return;
    }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String mobile = req.getParameter("mobile");
        String password = req.getParameter("password");
        JSONBean jsonBean=new JSONBean();
        User user = userDao.login(mobile);
        if (null==user){
            jsonBean.setFail("用户名或手机号错误");
        }else {
            if (!MD5Util.MD5(password).equals(user.getUser_password())){
                jsonBean.setFail("密码错误");
            }else {
                jsonBean.setSuccess("登录成功");
                req.getSession().setAttribute("user_id", user.getUser_id());
            }
        }
        resp.setContentType("application/json;charset=utf-8");
        String json = JSON.toJSONString(jsonBean);
        PrintWriter out = resp.getWriter();
        out.write(json);
        out.close();
        return;
    }
}
